/*给Vue实例添加原型属性，所有的Vue实例都拥有了$http属性*/
Vue.prototype.$http = axios;
axios.defaults.baseURL = "http://localhost";//换成你自己后台访问根URL
//这个参数，设置的是图片查看的全局服务器的地址
Vue.prototype.$fastBaseUrl = "http://47.95.117.210:8888";


/*axios前置拦截器， 只要你发送axios之前，都先执行此箭头函数*/
axios.interceptors.request.use(config => {
    //携带token
    let token = sessionStorage.getItem("token");
    if (token) {
        //添加请求头信息
        config.headers['token'] = token;
    }
    return config;
}, error => {
    Promise.reject(error);
});


//aixos后置拦截器，在响应目标函数之前，先执行此箭头函数
axios.interceptors.response.use(result => {
    let data = result.data;
    if (data.code === "1001") {
        //把token和登录用户删除掉
        sessionStorage.removeItem("token");
        //把登录用户删除吊
        sessionStorage.removeItem("userInfo");
        //跳转到登录界面
        location.href = "/login.html";
        return;
    }
    return result;
}, error => {
    Promise.reject(error);
});

/**
 * js动态获取?后面的参数，并且封装成一个json对象
 * @returns {Object}
 */
function getParam(){

    var url=location.search;
    console.log(url) //?openid=7c50233b78ba4b0b897f92df09202773&phone=17766663333
    var param = new Object();
    //
    if(url.indexOf("?")!=-1){ //index = 0

        var str = url.substr(1)
        console.log(str) //openid=7c50233b78ba4b0b897f92df09202773&phone=17766663333
        strs = str.split("&");
        console.log(strs) // Array ["openid=7c50233b78ba4b0b897f92df09202773","phone=17766663333"]
        for(var i=0;i<strs.length;i++){
            //"openid=7c50233b78ba4b0b897f92df09202773"
            var arr = strs[i].split("="); //["openid","7c50233b78ba4b0b897f92df09202773"]
            var key = arr[0] //"openid" "phone"
            var value = arr[1] //"7c50233b78ba4b0b897f92df09202773" "17766663333"
            console.log("key = "+key +" value =" +value)
            param[key]=unescape(value); //{"openid":"7c50233b78ba4b0b897f92df09202773","phone":"17766663333"}
        }
    }
    console.log(param)
    return param;//{"openid":"7c50233b78ba4b0b897f92df09202773","phone":"17766663333"}
}